
var moban8 = {
    // 全局变量
    moduleId:getQueryString('moduleId'),//模板id
    mbId:getQueryString('mbId'),//模板id
    widthHeight:{picWidth:'',picHeight:''},
    widthHeight2:{picWidth:'',picHeight:''},
    width:'',
    height:'',
    saveDataUrl:'',
    //specialInd:0,
    specialContent:'',//调用落地页位置
    index:parent.layer.getFrameIndex(window.name),
    formaData: new FormData(),
    picPosition: getQueryString('pos'),//在添加模板页 添加的模板的位置顺序
    params1 :{ //参数1
        subId:getQueryString('moduleId'),//专题页id
        modelUrl:'',//图片链接
        modelType:'8',//模块8
        contentIndex:'1',//图片排序
        modelStatus:'',//模板状态(是否展示):展示0:不展示1
        modelSpace:'',//模板间隔  1:间隔 0:不间隔
        placeType:'',//插入链接类型
        placeContent:'',//链接内容 落地页值
        userName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
    },
    params2 : { // 参数2
        subId:getQueryString('moduleId'),//专题页id
        modelUrl:'',//图片链接
        modelType:'8',//模块8
        contentIndex:'2',//图片排序
        modelStatus:'',//模板状态(是否展示):展示0:不展示1
        modelSpace:'',//模板间隔  1:间隔 0:不间隔
        placeType:'',//插入链接类型
        placeContent:'',//链接内容 落地页值
        userName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
    },
    params3:{ // 参数3
        subId:getQueryString('moduleId'),//专题页id
        modelUrl:'',//图片链接
        modelType:'8',//模块8
        contentIndex:'3',//图片排序
        modelStatus:'',//模板状态(是否展示):展示0:不展示1
        modelSpace:'',//模板间隔  1:间隔 0:不间隔
        placeType:'',//插入链接类型
        placeContent:'',//链接内容 落地页值
        userName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
    },
    params4:{ // 参数4
        subId:getQueryString('moduleId'),//专题页id
        modelUrl:'',//图片链接
        modelType:'8',//模块8
        contentIndex:'4',//图片排序
        modelStatus:'',//模板状态(是否展示):展示0:不展示1
        modelSpace:'',//模板间隔  1:间隔 0:不间隔
        placeType:'',//插入链接类型
        placeContent:'',//链接内容 落地页值
        userName:JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
    },
    init:function(){
        this.initData(moban8.mbId);//初始数据
        this.addClick();
    },
    // 点击事件的处理
    addClick:function(){
        // 上传图片的change事件
        $("#fileUpone").on('change',function(event){
            moban8.putDataToPage(event,this,1)
        });
        $("#fileUptwo").on('change',function(event){
            moban8.putDataToPage(event,this,2)
        });
        $("#fileUpthree").on('change',function(event){
            moban8.putDataToPage(event,this,3)
        });
        $("#fileUpfour").on('change',function(event){
            moban8.putDataToPage(event,this,4)
        });
        // 删除图片
        $(".deletfile1").on('click',function(){
          moban8.deletePic($(this),1)
        });
        $(".deletfile2").on('click',function(){
            moban8.deletePic($(this),2)
        });
        $(".deletfile3").on('click',function(){
            moban8.deletePic($(this),3)
        });
        $(".deletfile4").on('click',function(){
            moban8.deletePic($(this),4)
        })

        // 保存
        $('.save').on('click',function(){
            var modelUrl1 = $("#myImgone").attr('src');
            var modelStatus = $(".radionamebox").find('input:checked').val();
            var modelSpace = $(".radioretivalbox").find('input:checked').val();
            var specialInd1 = spcialLinkConfit.specialLinkArr[0].specialInd;//spcialLinkConfit 为上一级请求的js变量
            var specialContent1 = spcialLinkConfit.specialLinkArr[0].specialContent;
            var detailContent1 = spcialLinkConfit.specialLinkArr[0].detailContent;

            var modelUrl2 = $("#myImgtwo").attr('src');
            var specialInd2 = spcialLinkConfit.specialLinkArr[1].specialInd;
            var specialContent2 = spcialLinkConfit.specialLinkArr[1].specialContent;
            var detailContent2 = spcialLinkConfit.specialLinkArr[1].detailContent;

            var modelUrl3 = $("#myImgthree").attr('src');
            var specialInd3 = spcialLinkConfit.specialLinkArr[2].specialInd;
            var specialContent3 = spcialLinkConfit.specialLinkArr[2].specialContent;
            var detailContent3 = spcialLinkConfit.specialLinkArr[2].detailContent;

            var modelUrl4 = $("#myImgfour").attr('src');
            var specialInd4 = spcialLinkConfit.specialLinkArr[3].specialInd;
            var specialContent4 = spcialLinkConfit.specialLinkArr[3].specialContent;
            var detailContent4 = spcialLinkConfit.specialLinkArr[3].detailContent;
            moban8.params1.modelUrl = modelUrl1;
            moban8.params1.modelStatus= modelStatus;
            moban8.params1.modelSpace= modelSpace;
            moban8.params1.placeType= parseInt(specialInd1);
            moban8.params1.placeContent=specialContent1;
            moban8.params1.detailContent = detailContent1;


            moban8.params2.modelUrl = modelUrl2;
            moban8.params2.modelStatus= modelStatus;
            moban8.params2.modelSpace=modelSpace;
            moban8.params2.placeType=parseInt(specialInd2);
            moban8.params2.placeContent= specialContent2;
            moban8.params2.detailContent = detailContent2;

            moban8.params3.modelUrl = modelUrl3;
            moban8.params3.modelStatus= modelStatus;
            moban8.params3.modelSpace=modelSpace;
            moban8.params3.placeType=parseInt(specialInd3);
            moban8.params3.placeContent= specialContent3;
            moban8.params3.detailContent = detailContent3;


            moban8.params4.modelUrl = modelUrl4;
            moban8.params4.modelStatus= modelStatus;
            moban8.params4.modelSpace=modelSpace;
            moban8.params4.placeType=parseInt(specialInd4);
            moban8.params4.placeContent= specialContent4;
            moban8.params4.detailContent = detailContent4;


            var saveDataOne = [],saveDataTwo = [],saveDataThree = [],saveDataFour = [];
            saveDataOne.push(moban8.params1); //第一组保存值传给接口
            saveDataTwo.push(moban8.params2);// 第二组保存值传给接口
            saveDataThree.push(moban8.params3);//第三组保存值传给接口
            saveDataFour.push(moban8.params4);//第四组保存值传给接口
            var saveData = saveDataOne.concat(saveDataTwo,saveDataThree,saveDataFour);//保存接口的参数

            var newAry = [
                {id:specialInd1,url:modelUrl1,content:specialContent1,detailContent:detailContent1},
                {id:specialInd2,url:modelUrl2,content:specialContent2,detailContent:detailContent2},
                {id:specialInd3,url:modelUrl3,content:specialContent3,detailContent:detailContent3},
                {id:specialInd4,url:modelUrl4,content:specialContent4,detailContent:detailContent4}
            ];
            if(spcialLinkConfit.conditionJudge(newAry)){
                moban8.getSaveInfo(saveData)
            }

        });
        // 取消
        $('.cancel').on('click',function(){
            var curInd = layer.confirm(
                '<span>确认取消已编辑的模块</span>'+'<br/>', {title:'提示',btn: ['确定', '取消']},
                function(index){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    layer.close(curInd);
                },function(){
                    layer.close(curInd);
                });
        })
    },
    //初始数据
    initData:function(mbId){
        if(!!mbId){
            $(".change").html('模块编辑');
            var successCallBack = function(data){
                if(data.errorCode == 0){
                    // 页面回显
                    if(!!data.data && data.data.length > 0){
                        var imgHml = '';
                        $.each(data.data,function(index,item){
                            // 图片渲染
                            imgHml +='<li><img src="'+$(item)[0].modelUrl+'"></li>';
                            $(".radionamebox").find('input').each(function(index2,item2){
                                if($(item2).val() ==$(item)[0].modelStatus){
                                    $(item2).attr('checked','checked')
                                }
                            });
                            $(".radioretivalbox").find('input').each(function(index2,item2){
                                if($(item2).val() == $(item)[0].modelSpace){
                                    $(item2).attr('checked','checked')
                                }
                            });
                            moban8.widthInit($(item),index);
                        });
                        $(".img").html(imgHml);
                        spcialLinkConfit.specialDefaultInit();//上一个js的变量
                    }
                }else{
                    $.Huimodalalert('失败！',2000);
                }
            }
            return web.doAjax(root.special.searchModelMsg, 'post', {'modelArrayId':moban8.mbId,'subId':moban8.moduleId}, successCallBack)
        }
    },
    //图片类型校验
    picTypeCheck:function(file){
        var filePath = file.value;
        var flag = false;
        var fileTypes = [".jpg", ".png",".jpeg",".gif"];
        if(!!filePath){
            var fileEnd = filePath.substring(filePath.indexOf("."));
            flag = fileTypes.includes(fileEnd.toLowerCase());// 不区分图片后缀大小写格式
            if(!flag){
                $.Huimodalalert('图片格式支持jpg、jpeg、png、gif',2000);
                filePath = "";
                moban8.recoverFile()
            }
        }
        return flag;
    },
    // 图片大小验证
    picSizeCheck:function(file){
        var flag = false;
        var fileSize = 0,fileMaxSize = Number(1024 * 300),filePath = file.value;
        if(!!filePath){
            fileSize = file.files[0].size;
            if(fileSize > fileMaxSize){
                $.Huimodalalert('图片大小限制不能超过300KB',2000);
                filePath = "";
                flag = false;
                moban8.recoverFile()
            } else {
                flag = true;
            }
        }
        return flag;
    },
    //上传图片返显
    putDataToPage:function(e,file,num){
        var files = e.target.files;
        var reg = /^image\/.*$/i;
        var fileReader = new FileReader();//实例化FileReader API
        if(moban8.picTypeCheck(file) && moban8.picSizeCheck(file)){
            $.each(files,function(index,item){
                if(reg.test(item.type)){
                    fileReader.readAsDataURL(item);
                    fileReader.onload = function(e){
                        var data = e.target.result;
                        var img = new Image();
                        img.src = data;
                        img.onload = function(){
                            moban8.width = img.width;
                            moban8.height = img.height;
                            if(num == 1){ // 第一张图片
                                if(moban8.width != 750){
                                    $.Huimodalalert('图片尺寸限制为：宽度750px,高度不限',2000);
                                    item.value = "";
                                    moban8.recoverFile()
                                }else {
                                    moban8.submitPicUrl(file,e,num)
                                }
                            } else {
                                if( moban8.width != 250){
                                    $.Huimodalalert('图片尺寸限制为：宽度250px,高度不限',2000);
                                    item.value = "";
                                    moban8.recoverFile()
                                }else {
                                    moban8.submitPicUrl(file,e,num)
                                }
                            }

                        }
                    }
                }
            });
            moban8.formaData.append('file',$(file)[0].files[0])
        }

    },
    // 提交上传图片
    submitPicUrl:function(file,e,num){
        var formData = new FormData();
        formData.append('file',$(file)[0].files[0]);
        $.ajax({
            url:root.inter.imgUpload,
            cache: false,  //禁用缓存
            type: "POST",
            processData : false,//不去处理发送的数据
            contentType : false,//不去设置Content-Type请求头
            data: formData,
            success:function(data){
                $.each(data,function(index,item){
                    $(file).parent().parent().parent().find('img').removeClass('hide');
                    $(file).parent().parent().parent().find('img').attr("src", item.fullPath);
                    $(file).parent().find('.fileupone').html('更换图片');
                    moban8.widthHeight = {'picWidth':moban8.width,'picHeight':moban8.height};
                    moban8.recoverFile()
                    if(num == 1){
                        moban8.params1.picMsg = JSON.stringify(moban8.widthHeight)
                    } else if(num == 2){
                        moban8.params2.picMsg = JSON.stringify(moban8.widthHeight);
                    }else if(num == 3){
                        moban8.params3.picMsg = JSON.stringify(moban8.widthHeight);
                    }else {
                        moban8.params4.picMsg = JSON.stringify(moban8.widthHeight);
                    }
                })
                if(num == 1){
                    moban8.params1.modelUrl = data[0].fullPath;
                }else if(num == 2){
                    moban8.params2.modelUrl = data[0].fullPath;
                }else if(num == 3){
                    moban8.params3.modelUrl = data[0].fullPath;
                }else{
                    moban8.params4.modelUrl = data[0].fullPath;
                }
            }
        })
    },
    // 删除图片
    deletePic:function(e,num){
        if(num == 1){
            $("#myImgone").attr('src','');
        }else if(num == 2){
            $("#myImgtwo").attr('src','');
        }else if(num == 3){
            $("#myImgthree").attr('src','');
        }else {
            $("#myImgfour").attr('src','');
        }
        $(e).parent().parent().parent().find('img').attr('src','')
        $(e).parent().find('.fileupone').html('图片上传')
        moban8.recoverFile()
    },
    // 保存接口
    getSaveInfo:function(params){
        var dataurl = '';
        !!moban8.mbId ? dataurl = root.special.updateModel: dataurl = root.special.addAppModel;
        $.ajax({
            url:dataurl,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify(params),
            success:function(data){
                if(data.errorCode == 0){
                    !!moban8.mbId ? parent.specialEdit.singleModuleDagaGet(moban8.mbId,moban8.picPosition):parent.specialEdit.singleModuleDagaGet(data.data,moban8.picPosition);
                    layer.msg("保存成功",{icon: 6,time:1000},function () {
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        })
                    });
                }else{
                    $.Huimodalalert(data.msg,2000);
                }
            }
        })
    },
    //根据返回索引判断 图片宽高归属
    widthInit:function(item,index){
        if(!!item[0].picMsg){
            moban8.widthHeight2.picWidth = JSON.parse(item[0].picMsg).picWidth;
            moban8.widthHeight2.picHeight = JSON.parse(item[0].picMsg).picHeight;
            moban8.paramsPicMsg(item,index);
        } else {
            moban8.widthHeight2.picWidth = '';
            moban8.widthHeight2.picHeight = '';
            moban8.paramsPicMsg(item,index);
        }
    },
    paramsPicMsg:function(item,index){
        if(index == 0){
            $("#myImgone").attr('src',$(item)[0].modelUrl);
            !!moban8.mbId? moban8.params1.picMsg = JSON.stringify(moban8.widthHeight2) : moban8.params1.picMsg = JSON.stringify(moban8.widthHeight2);
            moban8.params1.modelId=$(item)[0].modelId;
            spcialLinkConfit.specialLinkArr[0].specialInd = item[0].placeType;
            spcialLinkConfit.specialLinkArr[0].specialContent =item[0].placeContent;
            spcialLinkConfit.specialLinkArr[0].detailContent =item[0].detailContent;
        } else if(index == 1){
            moban8.params2.modelId=$(item)[0].modelId;
            $("#myImgtwo").attr('src',$(item)[0].modelUrl);
            !!moban8.mbId ? moban8.params2.picMsg = JSON.stringify(moban8.widthHeight2):  moban8.params2.picMsg = JSON.stringify(moban8.widthHeight2);
            spcialLinkConfit.specialLinkArr[1].specialInd = item[0].placeType;
            spcialLinkConfit.specialLinkArr[1].specialContent =item[0].placeContent;
            spcialLinkConfit.specialLinkArr[1].detailContent =item[0].detailContent;
        }else if(index == 2){
            moban8.params3.modelId=$(item)[0].modelId;
            $("#myImgthree").attr('src',$(item)[0].modelUrl);
            !!moban8.mbId ? moban8.params3.picMsg = JSON.stringify(moban8.widthHeight2):  moban8.params3.picMsg = JSON.stringify(moban8.widthHeight2);
            spcialLinkConfit.specialLinkArr[2].specialInd = item[0].placeType;
            spcialLinkConfit.specialLinkArr[2].specialContent =item[0].placeContent;
            spcialLinkConfit.specialLinkArr[2].detailContent =item[0].detailContent;
        }else {
            moban8.params4.modelId=$(item)[0].modelId;
            $("#myImgfour").attr('src',$(item)[0].modelUrl);
            !!moban8.mbId ? moban8.params4.picMsg = JSON.stringify(moban8.widthHeight2):  moban8.params4.picMsg = JSON.stringify(moban8.widthHeight2);
            spcialLinkConfit.specialLinkArr[3].specialInd = item[0].placeType;
            spcialLinkConfit.specialLinkArr[3].specialContent =item[0].placeContent;
            spcialLinkConfit.specialLinkArr[3].detailContent =item[0].detailContent;
        }
    },
    // 上传图片失败后的文件类型恢复操作
    recoverFile:function(){
        $("#fileUpone").attr('type','text');
        $("#fileUpone").attr('type','file');
        $("#fileUptwo").attr('type','text')
        $("#fileUptwo").attr('type','file')
        $("#fileUpthree").attr('type','text');
        $("#fileUpthree").attr('type','file');
        $("#fileUpfour").attr('type','text')
        $("#fileUpfour").attr('type','file')
    }

}
moban8.init()